package com.shijunhao.graduation.service.business.impl;

import com.alibaba.fastjson.JSON;
import com.shijunhao.graduation.domain.bean.Keyword;
import com.shijunhao.graduation.domain.dto.BusinessPostDto;
import com.shijunhao.graduation.domain.entity.BusinessNote;
import com.shijunhao.graduation.domain.entity.SystemFileInfo;
import com.shijunhao.graduation.enums.TableName;
import com.shijunhao.graduation.mapper.BusinessNoteMapper;
import com.shijunhao.graduation.service.business.IBusinessHotInfoService;
import com.shijunhao.graduation.service.business.IBusinessPostService;
import com.shijunhao.graduation.service.system.ISystemFileInfoService;
import com.shijunhao.graduation.service.system.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: shijunhao
 * @date: 2022/5/22 21:38
 * @description:
 */
@Service
public class BusinessPostServiceImpl extends BaseServiceImpl<BusinessNoteMapper, BusinessNote> implements IBusinessPostService {

    @Resource
    private ISystemFileInfoService systemFileInfoService;
    @Resource
    private IBusinessHotInfoService hotInfoService;
    @Resource
    private SimilarAnalyzer similarAnalyzer;

    @Override
    public Long insert(BusinessPostDto dto) {
        BusinessNote entity = new BusinessNote();
        SystemFileInfo fileInfo = systemFileInfoService.upload(dto.getHtml());
        entity.setType(0);
        entity.setTitle(dto.getTitle());
        entity.setFileId(fileInfo.getId());
        //添加关键词信息
        List<Keyword> analyze = similarAnalyzer.analyze(dto.toString(), 5);
        entity.setKeywords(JSON.toJSONString(analyze));
        insertOne(entity);
        //插入热度信息
        hotInfoService.insertDefaultHotInfo(TableName.BUSINESS_NOTE.getLabel(), entity.getId());
        return entity.getId();
    }


}
